﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ATM
{
    public partial class addUser : Form
    {
        public addUser()
        {
            InitializeComponent();
        }

        private void exit_Click(object sender, EventArgs e)
        {
            welcom wForm = new welcom();
            this.Hide();
            wForm.Show();
        }

        private void back_Click(object sender, EventArgs e)
        {
            adminMainPanel ampForm = new adminMainPanel();
            this.Hide();
            ampForm.Show();
        }

        private void add_Click(object sender, EventArgs e)
        {
            if(nameVal.Text != "" && emailVal.Text != "" && anVal.Text != "" && balanceVal.Text != "")
            {
                string email = emailVal.Text;
                string name = nameVal.Text;
                if (email.IndexOf('@') == -1 || email.IndexOf('.') == -1)
                {
                    MessageBox.Show("invalid email adress!");
                }
                else if (name.Contains('1') || name.Contains('2') || name.Contains('3') || name.Contains('4') || name.Contains('5')
                    || name.Contains('6') || name.Contains('7') || name.Contains('8') || name.Contains('9') || name.Contains('0')
                    || name.Contains('_') || name.Contains('@') || name.Contains('%') || name.Contains('$') || name.Contains('*')
                    || name.Contains('!') || name.Contains('(') || name.Contains(')') || name.Contains('+') || name.Contains('-')
                    || name.Contains('/') || name.Contains('\\') || name.Contains('[') || name.Contains(']') || name.Contains('[')
                    || name.Contains('{') || name.Contains('}') || name.Contains('>') || name.Contains('<') || name.Contains(','))
                {
                    MessageBox.Show("the name field cantains invalid characters!");
                }
                else
                {
                    try
                    {
                        adminLogic al = new adminLogic();
                        string n = nameVal.Text;
                        string em = emailVal.Text;
                        int an = int.Parse(anVal.Text);
                        float b = float.Parse(balanceVal.Text);
                        int anTest = int.Parse(anVal.Text);
                        if(anTest <= 0)
                        {
                            MessageBox.Show("account number can not be less than or equal to 0 !");
                        }
                        else if (al.userExist(anTest) && al.emailExist(em))
                        {
                            if (al.addUser(n, em, an, b))
                            {
                                MessageBox.Show("user added");
                                adminMainPanel ampForm = new adminMainPanel();
                                this.Hide();
                                ampForm.Show();
                            }
                            else
                                MessageBox.Show("failed adding user");
                        }
                        else
                            MessageBox.Show("user accoun number or email already exist please try a different entries");
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message);}
                }
            }
            else
                MessageBox.Show("all the fields in this form are required!");
        }
    }
}
